package com.woniu.zsd.common.util;

import java.math.BigDecimal;
import java.math.MathContext;

public class DistanceUtil {
    private static final double EARTH_RADIUS = 6371.0; // 地球半径，单位为公里

    // 计算两点之间的距离，单位为公里
    public static double calculateDistance(BigDecimal lat1, BigDecimal lon1, BigDecimal lat2, BigDecimal lon2) {
        double radLat1 = Math.toRadians(lat1.doubleValue());
        double radLat2 = Math.toRadians(lat2.doubleValue());
        double radLon1 = Math.toRadians(lon1.doubleValue());
        double radLon2 = Math.toRadians(lon2.doubleValue());

        double dLat = radLat2 - radLat1;
        double dLon = radLon2 - radLon1;

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(radLat1) * Math.cos(radLat2) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return EARTH_RADIUS * c; // 返回公里数
    }
}